package com.example.system.class18;

import java.util.ArrayList;
import java.util.List;

public class Code02_StrFullPermutation {
    public static void main(String[] args) {
        System.out.println(fullPermutation("ABX"));
    }

    public static List<String> fullPermutation(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        char[] chars = str.toCharArray();
        List<String> ans = new ArrayList<>();
        process(chars, 0, ans);
        return ans;
    }

    // [0...index] 之前已经决定了的
    public static void process(char[] chars, int index, List<String> ans) {
        if (index == chars.length) { // base case
            ans.add(String.valueOf(chars));
            return;
        }

        // 遍历
        for (int i = index; i < chars.length; i++) {
            swap(chars, index, i);
            process(chars, index + 1, ans);
            swap(chars, index, i);
        }
    }


    private static void swap(char[] chars, int i, int j) {
        char t = chars[i];
        chars[i] = chars[j];
        chars[j] = t;
    }
}
